#{extends 'simpleTemplate.html' /}
<title>&{'Wiki_edit_wiki_page_header'}</title>
<script>

    //save and go to the show page
    function savePage() {
    if ($('#page_name').val() != "" && $('#page_text').val() != "" && $('#page_summary').val() != "" && $('#page_tags').val() != "") {

    $.get("@{wiki.WikiController.checkUniqueName()}", {"name": $('#page_name').val(), "id": "${current.id}"}, handleSavePage, "json");}
    else alert ("&{'Wiki_alert_not_fill_fields'}");
    }

    //function that receives check flag from server
    function handleSavePage(data, textStatus, jqXHR) {
    if(data.existName == 'true')
    {
    alert("&{'Wiki_alert_name_already_exists'}");
    }
    else
    {
    var content = $('#page_text').val();
    var summary = $('#page_summary').val();
    var tags = $('#page_tags').val();

    $.post("@{wiki.WikiController.updatePage}",
    {
    "id": "${current.id}",
    "name": $('#page_name').val(),
    "summary": summary,
    "content": content,
    "tags": tags
    },
    function() {
    document.location.href = "/showPage?id=" + "${current.id}";
    });
    }
    }

    //delete page and return to the wiki list
    function deletePage() {
    $.get("@{wiki.WikiController.deletePage}", {"id": "${current.id}"}, function() {
    document.location.href = "/wiki";
    });
    }


</script>
<div class="content">
    <div class="content-header">
        <h3>&{'Wiki_name'}: </h3>
        <textarea class="input-block-level" placeholder="&{'Wiki_add_name'}" id="page_name" rows="1">${current.name}</textarea>
    </div>

    <div class="content-body-light">

        <h3>&{'Wiki_summary'}: </h3>
        <div class="chat-input">
            <textarea class="input-block-level" placeholder="&{'Wiki_add_summary'}" id="page_summary">${current.summary}</textarea>
        </div>

        <h3>&{'Wiki_content'}: </h3>
        <div class="chat-input">
            <textarea class="input-block-level" placeholder="&{'Wiki_add_content'}" id="page_text" rows="15">${current.content}</textarea>
        </div>
        <h3>&{'Wiki_page_tags'}: </h3>
        <div class="chat-input">
            <textarea class="input-block-level" placeholder="&{'Wiki_add_page_tags'}" id="page_tags" rows="1">${current.getTagsString()}</textarea>
        </div>
        <input type="button" value="&{'Wiki_save'}" class="btn btn-primary" onclick="savePage()" id="save_button">
        <input type="button" value="&{'Wiki_delete'}" class="btn btn-primary" onclick="deletePage()" id="delete_button">
        <a class="btn btn-primary" href="/showPage?id=${current.id}&name=${current.name}&content=${current.content}">&{'Wiki_show'}</a>
    </div>
</div>